package com.digitalconsulting.phototraveler;

import java.util.List;

import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class ShowOnMapActivity extends MapActivity {

	MapView mapView;
	MapController mc;
	GeoPoint p;
	GeoPoint currentLocation;

	LocationManager locationManager;
	LocationListener locationListener;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.show_on_map_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.show_destination:
			mc.animateTo(p);
			mc.setZoom(17);
			mapView.invalidate();
			return true;



		default:
			return super.onOptionsItemSelected(item);
		}
	}

	class MapOverlay extends com.google.android.maps.Overlay {
		@Override
		public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
				long when) {
			super.draw(canvas, mapView, shadow);

			// ---translate the GeoPoint to screen pixels---
			Point screenPts = new Point();
			mapView.getProjection().toPixels(p, screenPts);

			// ---add the marker---
			Bitmap bmp = BitmapFactory.decodeResource(getResources(),
					R.drawable.locator_green);
			canvas.drawBitmap(bmp, screenPts.x - 25, screenPts.y - 86, null);

			/*
			if (currentLocation != null) {
				locationManager.removeUpdates(locationListener);

				Point screenPts2 = new Point();
				mapView.getProjection().toPixels(currentLocation, screenPts2);

				Bitmap bmp2 = BitmapFactory.decodeResource(getResources(),
						R.drawable.locator_red);
				canvas.drawBitmap(bmp2, screenPts2.x - 25, screenPts2.y - 86,
						null);

				int maxLatitude = Math.max(p.getLatitudeE6(),
						currentLocation.getLatitudeE6());
				int minLatitude = Math.min(p.getLatitudeE6(),
						currentLocation.getLatitudeE6());
				int maxLongitude = Math.max(p.getLongitudeE6(),
						currentLocation.getLongitudeE6());
				int minLongitude = Math.min(p.getLongitudeE6(),
						currentLocation.getLongitudeE6());

				 
			}*/
			//

			return true;
		}
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		// FindLocation f = new FindLocation();
		// f.execute(null);

		//
		// Get the location manager

		/*
		SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(getBaseContext());
		boolean showOnMap = sp.getBoolean("checkboxShowOnMap", true);

		if (showOnMap) {

			locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

			locationListener = new LocationListener() {
				public void onLocationChanged(Location location) {
					// Called when a new location is found by the network
					// location provider.

					currentLocation = new GeoPoint(
							(int) (location.getLatitude() * 1E6),
							(int) (location.getLongitude() * 1E6));

					mapView.invalidate();

					Toast toasat = Toast.makeText(getApplicationContext(),
							"Your location is founded.", Toast.LENGTH_LONG);
					toasat.show();

					// makeUseOfNewLocation(location);
				}

				public void onStatusChanged(String provider, int status,
						Bundle extras) {
				}

				public void onProviderEnabled(String provider) {
				}

				public void onProviderDisabled(String provider) {
				}
			};

			// if (sp.getBoolean("checkboxGPS", true))
			// locationManager.requestLocationUpdates(
			// LocationManager.GPS_PROVIDER, 0, 0, locationListener);

			// locationManager.requestLocationUpdates(
			// LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

		}*/

		Bundle bundle = this.getIntent().getExtras();
		String longitude = bundle.getString("longitude");
		String latitude = bundle.getString("latitude");

		setContentView(R.layout.show_on_map);

		mapView = (MapView) findViewById(R.id.mapView);
		mapView.setBuiltInZoomControls(true);

		mc = mapView.getController();
		String coordinates[] = { longitude, latitude };
		double lng = Double.parseDouble(coordinates[0]);
		double lat = Double.parseDouble(coordinates[1]);

		p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));

		mc.animateTo(p);
		mc.setZoom(17);
		mapView.invalidate();

		// ---Add a location marker---
		MapOverlay mapOverlay = new MapOverlay();
		List<Overlay> listOfOverlays = mapView.getOverlays();
		listOfOverlays.clear();
		listOfOverlays.add(mapOverlay);

		mapView.invalidate();

	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();

		if (locationManager != null)
			locationManager.removeUpdates(locationListener);
	}

}
